package com.tkjt.pms.calc.core;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

@Service
public class AuditStatusWatcher {

	/*****************************************************************************************
	 * *@KEY : 考核期
	 * *@value: 计算实例信息
	 * *@目前只支持: 一个后台计算任务
	 * 
	 */
	private Set<String> auditStatus = Collections.synchronizedSet(new HashSet<String>());

	public synchronized void startMirror(String mirrorCode, String indexGroup,String dataType) {
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append(mirrorCode).append("@");
		sbuilder.append(indexGroup).append("@");
		sbuilder.append(dataType).append("@");
		auditStatus.add(sbuilder.toString());
	}

	public synchronized void endMirror(String mirrorCode, String indexGroup,String dataType) {
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append(mirrorCode).append("@");
		sbuilder.append(indexGroup).append("@");
		sbuilder.append(dataType).append("@");
		auditStatus.remove(sbuilder.toString());
	}

	public synchronized boolean isOngoingMirror(String mirrorCode, String indexGroup,String dataType) {
		StringBuilder sbuilder = new StringBuilder();
		Set<String> indexGroups =new HashSet<String>();
		if(StringUtils.isEmpty(indexGroup)) {
			indexGroups.add("C");
			indexGroups.add("G");
		} else {
			indexGroups.add(indexGroup);
		}
		for(String oneIndexGroup : indexGroups) {
			sbuilder.append(mirrorCode).append("@");
			sbuilder.append(oneIndexGroup).append("@");
			sbuilder.append(dataType).append("@");
			if(auditStatus.contains(sbuilder.toString())) {
				return true; 
			}
		}
		return false;
	}

}
